import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/yhpc/YhpcViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';

class ChooseBuMen extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    // throw UnimplementedError();
    return ChooseBuMenState();
  }
}

class ChooseBuMenState extends BaseState<YhpcViewModel, ChooseBuMen> {
  var _data;
  List _selList = [];
  var _selData;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  _getData() {
    viewModel.getBuMenData("");
    viewModel.getBuMenDataStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _data = event['data'];
          _selList = _data;
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    return Scaffold(
      backgroundColor: SQColor.white,
      appBar: MTopBar(
        elevation: 0,
        title: '选择部门',
        rightButtonText: '确定',
        onRightButtonClick: () {
          Navigator.pop(context, {
            'orgCode': _selData['orgCode'],
            'orgName': _selData['orgName']
          });
        }
      ).create(),
      body: _data== null ? LoadingWidget() : _data.length == 0 ? NoneData() : _bodyView(),
    );
  }

  _bodyView() {
    return Container(
      margin: EdgeInsets.all(15),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _selData == null
              ? Container()
              : JText(
                  _selData['orgName'] ?? '',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
          Container(
            child: SingleChildScrollView(
              child: ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: _selList.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        setState(() {
                          _selData = _selList[index];
                          if (_selList[index]['children'] == null) {
                            return;
                          }
                          _selList = _selList[index]['children'];
                        });
                      },
                      child: Column(
                        children: [
                          Container(
                            padding: EdgeInsets.only(top: 15, bottom: 15),
                            child: Row(
                              children: [
                                Expanded(
                                  child: JText(_selList[index]['orgName']),
                                ),
                                Icon(
                                  Icons.arrow_forward_ios_outlined,
                                  size: Dimens.dimens15,
                                  color: SQColor.black_999999,
                                ),
                              ],
                            ),
                          ),
                          Container(
                            height: 1,
                            color: Color(0xfff6f6f6),
                            width: double.infinity,
                          ),
                        ],
                      ),
                    );
                  }),
            ),
          ),
        ],
      ),
    );
  }
}
